﻿using ConfExpert.Datasets;
using ConfExpert.Forms.Base;
using ConfExpert.Forms.Depo.Cikis;
using ConfExpert.Utility;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevTeam;

using System;
using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms.Depo
{
    //TODO: bunu da Transfer.cs ve YeniAyrim.cs ye benzet!

    public partial class Satis : XForm
    {
        MyData.xSepetLinesDataTable sel = new MyData.xSepetLinesDataTable();

        public Satis(MyData.xSepetLinesDataTable _selectedRolls)
        {
            InitializeComponent();
            sel = _selectedRolls;
        }

        public string rolls
        {
            get
            {
                List<int> rl = new List<int>();
                foreach (DataRow dr in sel.Rows)
                    rl.Add(dr["RollId"].ToInt());

                return string.Join(",", rl.ToArray());
            }
        }

        private void Satis_Load(object sender, EventArgs e)
        {
            lblAyrimlar.Text = ExecuteScalar("GetSatisAyrim", rolls).ToStr();

            GetDetails("SODetay", "GetOrderRef");

            gleOrderRef.Properties.Assign(GetIntellisensedRI(ds.SODetay));
            gleOrderRef.Properties.ProcessNewValue += new ProcessNewValueEventHandler(ri_ProcessNewValue2);
            gleOrderRef.Properties.View.Columns["BrandId"].ColumnEdit = RI(Rp.Brand);
            gleOrderRef.Properties.View.Columns["SezonId"].ColumnEdit = RI(Rp.Sezon);
            gleOrderRef.Closed += gleOrderRef_Closed;

            gleBrand.Properties.Assign(RI(Rp.Brand));
            gleSezon.Properties.Assign(RI(Rp.Sezon));
            gleOwner.Properties.Assign(RI(Rp.V_Customer));
            gleOwner.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;

            gleBrand.Closed += gleBrand_Closed;
            btnGo.Click += btnGo_Click;
            this.Shown += Satis_Shown;
        }

        void Satis_Shown(object sender, EventArgs e)
        {
            gleOwner.Focus();
        }

        void btnGo_Click(object sender, EventArgs e)
        {
            int CustomerId = gleOwner.EditValue.ToInt();
            if (CustomerId > 0)
            {
                if (intParse(gleBrand.EditValue) > 0)
                {
                    int CustomerofBrand = AppSession.Lookups.Brand.FindByBrandId(intParse(gleBrand.EditValue)).CustomerId;
                    if (CustomerId != CustomerofBrand)
                    {
                        XtraMessageBox.Show("Marka, seçtiğiniz müşterinin markası olmalı !");
                        return;
                    }
                }



                int SatisId = ExecuteScalar("InsKumasSatis", rolls, gleOrderRef.EditValue.ToStr(), gleBrand.EditValue.ToInt(), gleSezon.EditValue.ToInt(), gleOwner.EditValue.ToInt(), txtAciklama.EditValue.ToStr(), WhoAmI).ToInt();

                using (SatisFiyati f = new SatisFiyati(0, SatisId))
                {
                    f.ShowDialog();
                }

                Notify("Satış işlemi gerçekleşti.");
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                XtraMessageBox.Show("Yeni Malsahibi zorunlu alandır !");
                return;
            }

        }

        void gleBrand_Closed(object sender, ClosedEventArgs e)
        {
            switch (gleBrand.EditValue.ToInt())
            {
                case 1:
                case 290:
                    gleOrderRef.EditValue = null;
                    gleSezon.EditValue = 3;
                    gleSezon.Enabled = gleOrderRef.Enabled = false;
                    break;

                default:
                    gleSezon.Enabled = gleOrderRef.Enabled = true;
                    break;
            }
        }

        void gleOrderRef_Closed(object sender, ClosedEventArgs e)
        {
            switch (gleBrand.EditValue.ToInt())
            {
                case 1:
                case 290:
                    break;

                default:
                    if (gleOrderRef.Properties.View.GetFocusedRow() != null)
                    {
                        gleOrderRef.Properties.View.EndEditing();
                        DataRow dr = ((DataRowView)gleOrderRef.Properties.View.GetFocusedRow()).Row;
                        if (dr["Name"].ToStr() == gleOrderRef.EditValue.ToStr())
                        {
                            gleBrand.EditValue = intParse(dr["BrandId"]);
                            gleSezon.EditValue = intParse(dr["SezonId"]);
                        }
                    }
                    break;
            }
        }






    }
}
